package me.zhengjie.minbearwcs.dao.outbound.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import lombok.RequiredArgsConstructor;
import me.zhengjie.minbearwcs.dao.outbound.inf.OutboundOrderDao;
import me.zhengjie.minbearwcs.entity.Mapper.outbound.OutboundOrderMapper;
import me.zhengjie.minbearwcs.entity.PO.wcs.inbound.status.InboundOrderStatus;
import me.zhengjie.minbearwcs.entity.PO.wcs.outbound.OutboundOrder;
import me.zhengjie.minbearwcs.entity.PO.wcs.outbound.status.OutboundOrderStatus;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Repository
@RequiredArgsConstructor
public class OutboundOrderDaoImpl implements OutboundOrderDao {

    private final OutboundOrderMapper mapper;

    @Override
    public boolean insert(OutboundOrder outboundOrder) {
        return mapper.insert(outboundOrder) == 1;
    }

    @Override
    public List<OutboundOrder> selectByNeedStockUp(Integer needStockUp) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(OutboundOrder::getNeedStockUp, needStockUp)
                .list();
    }

    @Override
    public List<String> selectNeedStockUpOutboundOrder(OutboundOrderStatus status, Integer needStockUp) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(OutboundOrder::getStatus, status)
                .eq(OutboundOrder::getNeedStockUp, needStockUp)
                .orderByAsc(OutboundOrder::getPriority)
                .select(OutboundOrder::getOutboundOrderCode)
                .list()
                .stream()
                .map(OutboundOrder::getOutboundOrderCode)
                .collect(Collectors.toList());
    }

    @Override
    public List<OutboundOrder> selectInOutboundCodeOrder(List<String> outboundOrderCodeList) {
        return new LambdaQueryChainWrapper<>(mapper)
                .in(OutboundOrder::getOutboundOrderCode, outboundOrderCodeList)
                .orderByAsc(OutboundOrder::getPriority)
                .list();
    }

    @Override
    public OutboundOrderStatus selectStatusByOutboundOrderCode(String outboundOrderCode) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(OutboundOrder::getOutboundOrderCode, outboundOrderCode)
                .select(OutboundOrder::getStatus)
                .one()
                .getStatus();
    }

    @Override
    public boolean updateStartTimeByOutboundOrderCode(String outboundOrderCode, LocalDateTime startTime) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(OutboundOrder::getOutboundOrderCode, outboundOrderCode)
                .set(OutboundOrder::getStartTime, startTime)
                .update();
    }

    @Override
    public boolean updateStatusByOutboundOrderCode(String outboundOrderCode, OutboundOrderStatus status) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(OutboundOrder::getOutboundOrderCode, outboundOrderCode)
                .set(OutboundOrder::getStatus, status)
                .update();
    }

    @Override
    public boolean updatePriorityByOutboundOrderCode(String outboundOrderCode, Integer priority) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(OutboundOrder::getOutboundOrderCode, outboundOrderCode)
                .set(OutboundOrder::getPriority, priority)
                .update();
    }

    @Override
    public OutboundOrder selectByOrderCode(String orderCode) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(OutboundOrder::getOutboundOrderCode, orderCode)
                .one();
    }

    @Override
    public boolean deleteById(Integer id) {
        return mapper.deleteById(id) == 1;
    }

    @Override
    public boolean updateNeedStockUpByOutboundOrderCode(String outboundOrderCode, Integer needStockUp) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(OutboundOrder::getOutboundOrderCode,outboundOrderCode)
                .set(OutboundOrder::getNeedStockUp,needStockUp)
                .update();
    }
}
